package com.anlogic.sdk.openocd;

import com.anlogic.sdk.utils.EnvUtils;
import com.anlogic.sdk.utils.options.ChipType;

public class OpenocdInitConfig {
	public final static String PROGRAM = EnvUtils.getIDERootPath() + "/toolchain/openocd/fpsoc/bin/openocd";
	
	public final static String CFG_FILE_PRERFIX = EnvUtils.getIDERootPath() + "/toolchain/openocd/fpsoc/share/openocd/scripts/target";
	
	public final static String CMD_FORMAT = "%s %s -f \"%s\" %s";
	
	public final static String COMMAND_LINE_CMD_FROMAT = "-c \"%s\"";
	
	public final static String SET_INTI_PARAM_CMD = "set INIT_PARM 0x%x";
	
	public final static String INTI_CMD = "-c \"init\"";
	public final static String HALT_CMD = "-c \"halt\"";
	public final static String EXIT_CMD = "-c \"exit\"";
	
	public final static String ENABLE_PJTAG_CMD = "-c \"exec hw_cmd_tool \\\"%s/tools/data/enable_pjtag_%s.tcl\\\"\"";
	
	public final static String GDB_SERVER_CMD = "-c \"gdb_port %s\"";
	
	public final static String RBB_SERVER_CMD = "-c \"rbb server start %s\"";
	
	public static String getInitParamCmd(int val) {
		return String.format(SET_INTI_PARAM_CMD, val);
	}
	
	public static String getCmd(String preCmd, String cfgFile, String backCmd) {
		return String.format(CMD_FORMAT, PROGRAM, preCmd, cfgFile, backCmd);
	}
	
	public static String getCmdWithInit(String preCmd, String cfgFile, String backCmd) {
		backCmd = String.format("%s %s", INTI_CMD, backCmd);
		return String.format(CMD_FORMAT, PROGRAM, preCmd, cfgFile, backCmd);
	}
	
	public static String getCmdWithHalt(String preCmd, String cfgFile, String backCmd) {
		backCmd = String.format("%s %s %s", INTI_CMD, HALT_CMD, backCmd);
		return String.format(CMD_FORMAT, PROGRAM, preCmd, cfgFile, backCmd);
	}
	
	public static String getIndependentCmd(String preCmd, String cfgFile, String backCmd) {
		backCmd = String.format("%s %s %s %s", INTI_CMD, HALT_CMD, backCmd, EXIT_CMD);
		return String.format(CMD_FORMAT, PROGRAM, preCmd, cfgFile, backCmd);
	}
	
	public static String getCommandLineCmd(String cmd) {
		return String.format(COMMAND_LINE_CMD_FROMAT, cmd);
	}
	
	public static String getCfgFile(String chipName) {
		String cfgFile = "";
		if (ChipType.isValid(chipName)) {
			cfgFile = String.format("%s/anlogic/%s.cfg", CFG_FILE_PRERFIX, chipName);
			if (ChipType.isSOC66080(chipName)) {
				cfgFile = String.format("%s/soc/%s.cfg", CFG_FILE_PRERFIX, "apu");
			}
		}
		return cfgFile;
	}
	
	public static String getEnablePjtagCmd(String chipName) {
		if (ChipType.isDr1m90(chipName)) {
			return String.format(ENABLE_PJTAG_CMD, EnvUtils.getIDERootPath(), "apu");
		} else if (ChipType.isDr1v90(chipName)) {
			return String.format(ENABLE_PJTAG_CMD, EnvUtils.getIDERootPath(), "rpu");
		}
		return "";
	}
	
	public static String getGdbServerCmd(String port) {
		return String.format(GDB_SERVER_CMD, port);
	}
	
	public static String getRbbServerCmd(String port) {
		return String.format(RBB_SERVER_CMD, port);
	}
}
